import time

from selenium import webdriver

# 创建Chrome WebDriver
driver = webdriver.Chrome()

# 加载网址
driver.get("https://www.shanghairanking.cn/rankings/bcur/2021")
driver.maximize_window()
time.sleep(4)

while True:
    time.sleep(3)

    # 找到当前页的表格
    table = driver.find_element_by_xpath(
        "//div[@id='content']/div[@id='content-box']/div[@class='rk-table-box']/table[@class='rk-table']")

    # 提取每一行的数据
    rows = table.find_elements_by_tag_name("tr")
    for row in rows:
        columns = row.find_elements_by_tag_name("td")
        if len(columns) >= 2:
            university = columns[1].text
            print(f"大学名称: {university}")

    # 查找下一页按钮
    next_button = driver.find_element_by_css_selector("li.ant-pagination-next")

    # 如果下一页按钮不可点击，说明已经到了最后一页，退出循环
    if "ant-pagination-disabled" in next_button.get_attribute("class"):
        break

    # 否则，点击下一页按钮
    next_button.click()

    # 等待一段时间，让下一页加载完毕
    time.sleep(3)

# 关闭浏览器
driver.quit()
